package com.wbs.shop.nuby.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.databinding.DialogCashierPayShowBinding;

import java.math.BigDecimal;

public class CashierPayShowDialog extends Dialog {
    private DialogCashierPayShowBinding mBinding;
    private View.OnClickListener mOnClickListener;
    private String payAmount;
    private boolean isPad;

    public CashierPayShowDialog(@NonNull Context context, boolean isPad, String payAmount, View.OnClickListener mOnClickListener) {
        super(context, R.style.loadingdialog);
        this.payAmount = payAmount;
        this.isPad = isPad;
        this.mOnClickListener = mOnClickListener;
        init();
    }

    private void init() {
        mBinding = DialogCashierPayShowBinding.inflate(LayoutInflater.from(getContext()));
        setContentView(mBinding.getRoot());
        mBinding.tvYingShou.setText(payAmount);
        mBinding.tvShiShou.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    mBinding.tvZhaoHui.setText(String.valueOf(new BigDecimal(s.toString()).subtract(new BigDecimal(payAmount)).doubleValue()));
                } catch (Exception e) {
                    Log.e("Exception", "", e);
                }
            }
        });
        mBinding.tvConfirm.setOnClickListener(v -> {
            dismiss();
            if (mOnClickListener != null) {
                mOnClickListener.onClick(v);
            }
        });
        if (isPad) {
            WindowManager.LayoutParams attributes = getWindow().getAttributes();
            attributes.width = getContext().getResources().getDisplayMetrics().widthPixels / 3;
            getWindow().setAttributes(attributes);
            getWindow().setGravity(Gravity.CENTER);
        } else {
            WindowManager.LayoutParams attributes = getWindow().getAttributes();
            attributes.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8);
            getWindow().setAttributes(attributes);
            getWindow().setGravity(Gravity.CENTER);
        }
    }
}
